<script setup lang="ts">
import { ref, onMounted, onBeforeUnmount } from 'vue'
import { HubConnectionBuilder } from '@microsoft/signalr';
import { piniaData } from '@/store';//引入pinia状态管理
const store = piniaData()
onMounted(() => {
  // i()'
  startConnection();
})
const s: any = ref()

const connectionStatus = ref('连接中...');
// const i = () => {
//   "WebSocket" in window ? s.value = new WebSocket(`ws://192.168.10.72:7080`) : console.log("当前浏览器 Not support websocket"),
//     s.value.onerror = function () {
//       console.log("连接失败")
//     },
//     s.value.onopen = function () {
//       console.log("连接成功")
//     },
//     s.value.onmessage = function (d: any) {
//       const c = JSON.parse(d.data);
//       console.log(c);
//     }
// }
const messages: any = ref([]);
let connection: any = null;
const startConnection = async () => {
  try {
    connection = new HubConnectionBuilder()
      // .withUrl('http://10.0.0.50:44351/agvInfoHub?agvId=agvsInfo') // 替换为实际的 SignalR 服务器 URL  
      .withUrl('http://192.168.0.142:5003/chathub') // 替换为实际的 SignalR 服务器 URL  
      .withAutomaticReconnect()
      .build();

    await connection.start();
    console.log('SignalR 已连接');

    connection.on('RealtimeAGVInfo', (message) => {
      // messages.value.push(message);
      store.messages = message;
      console.log(message, '我是接收到的SignalR信息');

    });
  } catch (error) {
    console.error('SignalR 连接失败: ', error);
  }
};
onBeforeUnmount(() => {
  if (connection) {
    connection.stop();
  }
}); 
</script>

<template>
  <dv-full-screen-container>
    <router-view></router-view>
  </dv-full-screen-container>
</template>

<style>
#app {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}
</style>
